package com.fyzk.activity.lottery.jc.football.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.lottery.jc.football.JCInfo;
import com.fyzk.activity.lottery.jc.football.JZBetContentUtil;
import com.fyzk.activity.lottery.jc.football.JZBetNumUtil;
import com.fyzk.activity.zhifu.JCBasePayActivity;
import com.fyzk.interfaces.OnFootballPayTeamDeleteListener;
import com.tyjc.lottery.R;
import com.fyzk.net.PublicMethod;
import com.fyzk.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

import static com.fyzk.activity.lottery.jc.football.guoguan.JC_GuoGuanDialog.result;

/**
 * 初竞彩让球和胜平负 之外需要弹框选择比赛的Adapter
 *
 * @author miao
 */
public class JCZQ_BQC_ZhiFuAdapter extends JCZQZhiFuBaseAdapter {

    private static final String TAG = "JCBF_ZhiFuAdapter";
    private boolean isDan;
    private LayoutInflater mInflater;
    private Handler handler;
    private int zhuShu;
    private OnFootballPayTeamDeleteListener listener;
    private int danCount = 0;
    private String chuanStr = result;
    private boolean isHG; // 是否是混合过关

    public JCZQ_BQC_ZhiFuAdapter(Context context, boolean type, List<JCInfo> infors, Handler
            handler, OnFootballPayTeamDeleteListener listener) {
        this.isDan = type;
        this.infors = getInfor(infors);
        mInflater = LayoutInflater.from(context);
        this.handler = handler;
        this.listener = listener;
        refreshDan();
    }

    @Override
    public int getCount() {
        return infors.size();
    }

    @Override
    public Object getItem(int position) {
        return infors.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.jz_bqc_zhifu_listview_item, null);
            holder = new ViewHolder();
            holder.zhu = convertView.findViewById(R.id.jcbf_zhifu_list_item_zhu);
            holder.delete = convertView.findViewById(R.id.jcbf_zhifu_list_item_delete);
            holder.home = convertView.findViewById(R.id.jcbf_zhifu_list_item_tv_home);
            holder.week_num = convertView.findViewById(R.id.week_num);
            holder.away = convertView.findViewById(R.id.jcbf_zhifu_list_item_tv_away);
            holder.dan = convertView.findViewById(R.id.jcbf_zhifu_list_item_dan);
            holder.bqc_ss = convertView.findViewById(R.id.bqc_team_item_ss);
            holder.bqc_sp = convertView.findViewById(R.id.bqc_team_item_sp);
            holder.bqc_sf = convertView.findViewById(R.id.bqc_team_item_sf);
            holder.bqc_ps = convertView.findViewById(R.id.bqc_team_item_ps);
            holder.bqc_pp = convertView.findViewById(R.id.bqc_team_item_pp);
            holder.bqc_pf = convertView.findViewById(R.id.bqc_team_item_pf);
            holder.bqc_fs = convertView.findViewById(R.id.bqc_team_item_fs);
            holder.bqc_fp = convertView.findViewById(R.id.bqc_team_item_fp);
            holder.bqc_ff = convertView.findViewById(R.id.bqc_team_item_ff);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        JCInfo infor = infors.get(position);
        if (isDan || isHG) {
            holder.dan.setVisibility(View.GONE);
        } else {
            holder.dan.setVisibility(View.VISIBLE);
            holder.dan.setChecked(infor.isDan());
            if (infor.isDaneble()) {
                holder.dan.setEnabled(true);
                holder.dan.setTag(R.string.jc_info, infor);
                holder.dan.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        JCInfo infor = (JCInfo) v.getTag(R.string.jc_info);
                        infor.setDan(!infor.isDan());
                        refreshDan();
                    }
                });
            } else {
                holder.dan.setEnabled(false);
            }
        }
        holder.zhu.setVisibility(View.VISIBLE);
        holder.home.setText(infor.getHomeTeam());
        holder.away.setText(infor.getAwayTeam());
        holder.week_num.setText(infor.getOfficialWeekDay() + PublicMethod.getTeamNum(infor.getMatchNum()));
        JCInfo.SPInfo SpSS = infor.btnHGPojo.get(infor.BQC_33);
        JCInfo.SPInfo SpSP = infor.btnHGPojo.get(infor.BQC_31);
        JCInfo.SPInfo SpSF = infor.btnHGPojo.get(infor.BQC_30);
        JCInfo.SPInfo SpPS = infor.btnHGPojo.get(infor.BQC_13);
        JCInfo.SPInfo SpPP = infor.btnHGPojo.get(infor.BQC_11);
        JCInfo.SPInfo SpPF = infor.btnHGPojo.get(infor.BQC_10);
        JCInfo.SPInfo SpFS = infor.btnHGPojo.get(infor.BQC_03);
        JCInfo.SPInfo SpFP = infor.btnHGPojo.get(infor.BQC_01);
        JCInfo.SPInfo SpFF = infor.btnHGPojo.get(infor.BQC_00);
        holder.bqc_ss.setText("胜胜 " + SpSS.spValue);
        holder.bqc_sp.setText("胜平 " + SpSP.spValue);
        holder.bqc_sf.setText("胜负 " + SpSF.spValue);
        holder.bqc_ps.setText("平胜 " + SpPS.spValue);
        holder.bqc_pp.setText("平平 " + SpPP.spValue);
        holder.bqc_pf.setText("平负 " + SpPF.spValue);
        holder.bqc_fs.setText("负胜 " + SpFS.spValue);
        holder.bqc_fp.setText("负平 " + SpFP.spValue);
        holder.bqc_ff.setText("负负 " + SpFF.spValue);

        // 判断是否开售(赔率为空,禁止点击)
        holder.bqc_ss.setChecked(SpSS.isSel);
        holder.bqc_sp.setChecked(SpSP.isSel);
        holder.bqc_sf.setChecked(SpSF.isSel);
        holder.bqc_ps.setChecked(SpPS.isSel);
        holder.bqc_pp.setChecked(SpPP.isSel);
        holder.bqc_pf.setChecked(SpPF.isSel);
        holder.bqc_fs.setChecked(SpFS.isSel);
        holder.bqc_fp.setChecked(SpFP.isSel);
        holder.bqc_ff.setChecked(SpFF.isSel);
        holder.bqc_ss.setTag(SpSS);
        holder.bqc_sp.setTag(SpSP);
        holder.bqc_sf.setTag(SpSF);
        holder.bqc_ps.setTag(SpPS);
        holder.bqc_pp.setTag(SpPP);
        holder.bqc_pf.setTag(SpPF);
        holder.bqc_fs.setTag(SpFS);
        holder.bqc_fp.setTag(SpFP);
        holder.bqc_ff.setTag(SpFF);
        holder.bqc_ss.setTag(R.string.jc_info, infor);
        holder.bqc_sp.setTag(R.string.jc_info, infor);
        holder.bqc_sf.setTag(R.string.jc_info, infor);
        holder.bqc_ps.setTag(R.string.jc_info, infor);
        holder.bqc_pp.setTag(R.string.jc_info, infor);
        holder.bqc_pf.setTag(R.string.jc_info, infor);
        holder.bqc_fs.setTag(R.string.jc_info, infor);
        holder.bqc_fp.setTag(R.string.jc_info, infor);
        holder.bqc_ff.setTag(R.string.jc_info, infor);
        holder.bqc_ss.setOnClickListener(scoreClickListener);
        holder.bqc_sp.setOnClickListener(scoreClickListener);
        holder.bqc_sf.setOnClickListener(scoreClickListener);
        holder.bqc_ps.setOnClickListener(scoreClickListener);
        holder.bqc_pp.setOnClickListener(scoreClickListener);
        holder.bqc_pf.setOnClickListener(scoreClickListener);
        holder.bqc_fs.setOnClickListener(scoreClickListener);
        holder.bqc_fp.setOnClickListener(scoreClickListener);
        holder.bqc_ff.setOnClickListener(scoreClickListener);
        holder.delete.setTag(R.string.jc_info, infor);
        holder.delete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                JCInfo infor = (JCInfo) v.getTag(R.string.jc_info);
                if (listener != null) {
                    listener.onDelete(infor);
                }
                JCBasePayActivity.infos.remove(infor);
                setInfors(JCBasePayActivity.infos, isDan);
            }
        });
        jiSuanZhuShu();
        return convertView;
    }

    /**
     * 获得支付list数据
     *
     * @param infors
     * @return
     */
    private List<JCInfo> getInfor(List<JCInfo> infors) {
        ArrayList<JCInfo> list = new ArrayList<>();
        for (int i = 0; i < infors.size(); i++) {
            JCInfo info = infors.get(i);
            if (info.getOnclikNum() > 0) {
                list.add(info);
            }
        }
        JCBasePayActivity.infos = list;
        return list;
    }

    /**
     * 计算注数（待修改）
     */
    public void jiSuanZhuShu() {
        List<Integer> clicknums = new ArrayList<>();
        List<Boolean> dannums = new ArrayList<>();
        zhuShu = 0;
        int changCi = 0;
        int wanFa = 1;
        for (int i = 0; i < infors.size(); i++) {
            JCInfo info = infors.get(i);
            int xuan_count = 0;
            if (info.getOnclikNum() > 0) {
                changCi++;
                xuan_count = info.getOnclikNum();
            }
            clicknums.add(xuan_count);
            if (info.isDan()) {
                wanFa = 2;
            }
            dannums.add(info.isDan());// 添加胆拖
        }
        List<Integer> chuans = new ArrayList<Integer>();
        if (isDan) {
            for (int i = 0; i < clicknums.size(); i++) {
                zhuShu += clicknums.get(i);
            }
        } else {
            String chuan = result;
            if (chuan.contains(",") || chuan.endsWith("串1")){
                if (chuan.contains("串")) {
                    String[] chuan_array = chuan.split(",");
                    for (int i = 0; i < chuan_array.length; i++) {
                        String chuan_i = chuan_array[i];
                        chuans.add(Integer.valueOf(chuan_i.charAt(0) + ""));
                    }

                    for (Integer i : chuans) {
                        zhuShu += JZBetNumUtil.getZhushu(i, clicknums, dannums);
                    }
                }
            } else if (chuan.contains("串")) {
                String[] split = chuan.split("串");
                int teamNum = Integer.valueOf(split[0]);
                int select = Integer.valueOf(split[1]);
//                zhuShu = JZBetNumUtil.getDuoZhushu(teamNum, select, dannums, clicknums);
//                // L场比赛选择M关的可能性为groupNum种组合情况
//                int groupNum = JZBetNumUtil.getZhushu(teamNum, clicknums, dannums);

                String betcode = JZBetContentUtil.getContentBQC(infors, isDan, chuan);
                zhuShu = JZBetNumUtil.caculateZhushu(chuan, betcode, teamNum, isDan, dannums);

            }
        }

        Message msg = handler.obtainMessage();
        msg.obj = zhuShu;
        msg.arg1 = changCi;
        msg.arg2 = wanFa;
        msg.what = JCBasePayActivity.ZF_WHAT1;
        handler.sendMessage(msg);
    }

    public List<JCInfo> getInfors() {
        return infors;
    }

    public void setInfors(List<JCInfo> infors, boolean isdanguan) {
        this.isDan = isdanguan;
        this.infors = infors;
        this.notifyDataSetChanged();
        LogUtil.e("支付的列表长度：>"+infors.size());
        if(infors.size()==0){
            jiSuanZhuShu();
        }
    }

    View.OnClickListener scoreClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onScoreClick(v);
        }
    };

    private void onScoreClick(View view) {
        JCInfo.SPInfo spInfo = (JCInfo.SPInfo) view.getTag();
        JCInfo infor = (JCInfo) view.getTag(R.string.jc_info);
        boolean isFail = spInfo.isSel;
        spInfo.isSel = !isFail;
        updateTouMsg(infor, spInfo.isSel);
    }


    private void updateTouMsg(JCInfo infor, boolean isChecked) {
        if (isChecked) {
            infor.setOnclikNum(infor.getOnclikNum() + 1);
        } else {
            infor.setOnclikNum(infor.getOnclikNum() - 1);
        }
        if (infor.getOnclikNum() == 0) {
            if (listener != null) {
                listener.onDelete(infor);
            }
        }
        jiSuanZhuShu();
    }


    class ViewHolder {
        LinearLayout zhu;
        LinearLayout wei;
        ImageView delete;
        TextView week_num;
        TextView home;
        TextView away;
        CheckBox dan;
        public CheckBox bqc_ss;
        public CheckBox bqc_sp;
        public CheckBox bqc_sf;
        public CheckBox bqc_ps;
        public CheckBox bqc_pp;
        public CheckBox bqc_pf;
        public CheckBox bqc_fs;
        public CheckBox bqc_fp;
        public CheckBox bqc_ff;
    }

}
